package syscontrollers

import (
	"myadmin/sys/session"
	"myadmin/sys/syscontrollers/syscommon"
	"myadmin/sys/sysmodels"
)

type ViewLogin struct {
	syscommon.BaseController
}

func (c *ViewLogin) Get() {
	c.TplName = "login.html"
	c.Render()
}

//登录
func (this *ViewLogin) Post(){
	//登录密码验证
	var user = sysmodels.NewSysUser()
	err:=this.ParseForm(user)

	if err != nil {
		this.JsonRep500x("参数错误", err)
		return
	}
	if user.Username == ""{
		this.JsonRep500("缺少账号")
		return
	}
	if user.Password == ""{
		this.JsonRep500("缺少密码")
		return
	}

	ok,err := user.CheckPassword()
	if !ok {
		this.JsonRep500x(err.Error(),err)
		return
	}
	//生成token
	sel:=session.NewSession()
	sel.User = *user
	token:=session.Ses.Add(sel)
	//fmt.Println(token)

	this.Ctx.Output.Header("Authorization", token) //发送前端
	this.JsonRep200x("登录成功", map[string]string{"token": token})
}

